package cn.walls1717.wallsmall.product.app;

import cn.hutool.core.bean.BeanUtil;
import cn.walls1717.wallsmall.common.utils.PageUtils;
import cn.walls1717.wallsmall.common.utils.R;
import cn.walls1717.wallsmall.product.entity.CategoryBrandRelationEntity;
import cn.walls1717.wallsmall.product.service.CategoryBrandRelationService;
import cn.walls1717.wallsmall.product.vo.BrandVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 品牌分类关联
 *
 * @author walls1717
 * @email walls1717@163.com
 * @date 2022-10-21 11:58:42
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Resource
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 获取分类关联的品牌
     */
    @GetMapping("/brands/list")
    public R listBrandsByCatId(@RequestParam(value = "catId", defaultValue = "-1") Long catId) {
        // 前端没有传catId，那么直接返回空
        if (catId == -1) {
            return R.ok().put("data", null);
        }
        List<CategoryBrandRelationEntity> catalogBrandRelations = categoryBrandRelationService.listBrandsByCatId(catId);
        // 前端传入catId，根据catId查询catelog_brand_relation表，并将数据封装成VO返回
        List<BrandVO> brandVOS = catalogBrandRelations.stream().map(catalogBrandRelation -> {
            BrandVO brandVO = new BrandVO();
            BeanUtil.copyProperties(catalogBrandRelation, brandVO);
            return brandVO;
        }).collect(Collectors.toList());
        return R.ok().put("data", brandVOS);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 获取当前品牌关联的所有分类列表
     */
    @GetMapping("/catelog/list")
    // @RequiresPermissions("product:categorybrandrelation:list")
    public R listCategoryBrandRelations(@RequestParam() Long brandId) {
        List<CategoryBrandRelationEntity> data =
            categoryBrandRelationService.lambdaQuery().eq(CategoryBrandRelationEntity::getBrandId, brandId).list();
        return R.ok().put("data", data);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id) {
        CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    // @RequiresPermissions("product:categorybrandrelation:save");
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.saveDetail(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids) {
        categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
